package com.itheima.controller;

import com.itheima.pojo.domain.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门信息
     * @return
     */

    @GetMapping
    public Result selAll(){
        log.info("查询所有部门信息");
        List<Dept> DeptInfo = deptService.list();
        return Result.success(DeptInfo);
    }

    /**
     * 添加部门信息
     * @param dept
     * @return
     */
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门信息");
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 修改部门信息
     * @param dept
     * @return
     */
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门信息");
        deptService.update(dept);
        return Result.success();
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @DeleteMapping()
    public Result deleteDept(@RequestParam Integer id){
        log.info("删除部门信息");
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getDeptInfo(@PathVariable Integer id){
        log.info("根据id查询");
        Dept depts = deptService.findById(id);
       return Result.success(depts);
    }
}
